package com.screx.mcloud.lib.view

import android.content.Context
import android.content.Intent
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.LayoutRes
import com.alibaba.android.arouter.launcher.ARouter
import com.ba.base.R
import com.ba.base.api.ApiAuth
import com.ba.base.utils.SharedPreUtils
import com.ba.base.utils.ThemeHelper
import com.ba.base.view.EnumTheme
import com.ba.base.view.ThemeData
import com.ba.base.view.dialog.ProgressDialog
import com.ba.base.view.net.NetStateChangeReceiver
import com.ba.base.view.net.NetworkType
import com.ba.base.view.net.NetworkUtil
import com.ba.base.view.state.IViewState
import com.ba.base.view.status.StatusView
import com.ba.base.view.status.StatusViewBuilder
import com.ba.base.view.web.H5WebChromeClient
import qsos.lib.base.base.activity.BaseActivity

abstract class AbsThemeActivity : BaseActivity(), IViewState, NetworkUtil.NetStateChangeObserver {

    var mStateView: StatusView? = null
    var mStateBuilderDef: StatusViewBuilder.Builder? = null
    private var dialog: ProgressDialog? = null
    abstract fun notifyTheme(data: ThemeData)

    var mToolbar: View? = null
    var mTitle: TextView? = null
    var mTitleRight: TextView? = null
    var mBack: ImageView? = null
    var mH5WebChromeClient: H5WebChromeClient? = null

    override fun getData() {}

    override fun initData(savedInstanceState: Bundle?) {
    }

    override fun initView() {
        NetStateChangeReceiver.registerReceiver(this)

//        setTheme()

        mToolbar = findViewById(R.id.app_toolbar)
        mTitle = findViewById(R.id.app_title)
        mTitleRight = findViewById(R.id.app_title_right)
        mTitleRight?.let {
        }
        mBack = findViewById(R.id.app_back)

        mBack?.setOnClickListener {
            finish()
        }

//        ThemeHelper.mThemeData.observe(this, Observer {
//            notifyTheme(it)
//        })

        mStateBuilderDef = StatusViewBuilder.Builder().setOnEmptyRetryClickListener {
            showLoading()
            getData()
        }.setOnErrorRetryClickListener {
            showLoading()
            getData()
        }.setOnLoginClickListener {//跳转到登录页面
            showError()
            ARouter.getInstance().build("/app/login")
                .navigation()
        }

    }

    override fun onStart() {
        super.onStart()
    }

    override fun onStop() {
        super.onStop()
    }

    override fun onResume() {
        NetStateChangeReceiver.registerObserver(this)

        super.onResume()
//        setTheme()
//        ThemeHelper.setColor(
//            mContext as Activity, Color.parseColor("#FFFFFF"), 0
//        )
//        ThemeHelper.setLightMode(mContext as Activity)
    }

    override fun onPause() {
        super.onPause()
        NetStateChangeReceiver.unRegisterObserver(this)
    }

    override fun onDestroy() {
        NetStateChangeReceiver.unRegisterReceiver(this)
        super.onDestroy()
    }

    override fun showEmpty() {
        mStateView?.showEmptyView()
    }

    override fun showError(msg: String?) {
        Handler {
            mStateView?.showErrorView()
            false
        }.sendEmptyMessageDelayed(0, 500)
    }

    override fun showLoading() {
        mStateView?.showLoadingView()
    }

    override fun showSuccess() {
        mStateView?.showContentView()
    }

    override fun showLoginout() {//登录过期
        mStateView?.showLoginView()
        clear(this)
    }

    private fun clear(context: Context) {
        SharedPreUtils.clearUserInfo(context)
        ApiAuth.status = ApiAuth.STATUS.NULL
        ApiAuth.bearer = ""
        ApiAuth.userId = ""
        ApiAuth.deptId = -1
        ApiAuth.phone = ""
        ApiAuth.user = null
    }


    private fun setTheme() {
        val color = SharedPreUtils.getTheme(this)
        setThemeWithColor(color)
    }

    private fun setThemeWithColor(color: Int) {
        val type = EnumTheme.type(color)
        mToolbar?.setBackgroundColor(color)
        ThemeHelper.setColor(this, color, 0)
        ThemeHelper.mThemeData.postValue(ThemeData(type, type == EnumTheme.GRAY))
    }

    fun setBack(drawable: Drawable) {
        mBack?.setImageDrawable(drawable)
    }

    fun setTitleName(title: String) {
        mTitle?.text = title
    }

    fun setRight(title: String) {
        mTitleRight?.visibility = View.VISIBLE
        mTitleRight?.text = title
    }


    fun getViewById(@LayoutRes id: Int, parent: ViewGroup? = null): View =
        LayoutInflater.from(mContext).inflate(id, parent, false)


    override fun onNetDisconnected() {
        NetStateChangeReceiver.mNetworkType = NetworkType.NETWORK_NO
    }

    override fun onNetConnected(networkType: NetworkType) {
        NetStateChangeReceiver.mNetworkType = networkType
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == H5WebChromeClient.requestCode) {
            if (resultCode == RESULT_OK) {
                mH5WebChromeClient?.webFileCallback(data?.let { data.data })
            } else if (resultCode == RESULT_CANCELED) {
                mH5WebChromeClient?.webFileCallback(null)
            }
        }
    }

    fun showProgress() {
        if (dialog == null) {
            dialog = ProgressDialog(this).apply {
                setText("加载中...")
            }
        }
        dialog?.show()
    }

    fun dismissProgress() {
        dialog?.dismiss()
    }
}